//
//  NewsViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "NewsViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

-(void)dealloc
{
    
    [_myArray release];
    [_scroll release];
    [_myScroll release];
    [_myView release];
    [_myTableV release];
    [_array release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor  = [UIColor whiteColor];
    
    self.myTableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height-64-49) style:UITableViewStylePlain];
    self.myTableV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableV];
    self.navigationItem.title = @"资讯新闻";
    self.myTableV.delegate = self; //设置tableView基本配置
    self.myTableV.dataSource = self;
    [_myTableV release];
    
    
    
    self.array = [NSMutableArray array];
    
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.255)];
    self.myView.backgroundColor = [UIColor redColor];
    //    创建头部区域
    self.myTableV.tableHeaderView = self.myView;
    [_myView release];
    
    
    
    self.myScroll = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height*0.255)];
    self.myScroll.youDelegate =self;
    self.myScroll.backgroundColor = [UIColor whiteColor];
    [self.myView addSubview:self.myScroll];

    
    
    
    [self create];
   
    self.myTableV.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(careate)];
   

    
    
   
    self.number = 1;
    [self addfooter];
    [self  getInformation];
  
}


-(void)create
{
    
    self.myArray = [NSMutableArray array];
 
    [[HeroBaseHandle ShareHeroHeroDate]openDB];
    

    
    if ([[HeroBaseHandle ShareHeroHeroDate] selectScroll] != nil) {
        
        self.myArray = [[HeroBaseHandle ShareHeroHeroDate] selectScroll];
    }
    [AFN AFNGetData:@"http://lolbox.oss.aliyuncs.com/json/v3/news/banner.json" andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            [self.myArray removeAllObjects];
            
            [self.myScroll removeFromSuperview];
            [[HeroBaseHandle ShareHeroHeroDate]openDB];
            
            [[HeroBaseHandle ShareHeroHeroDate] dropScrollTable ];
            
            [[HeroBaseHandle ShareHeroHeroDate]createScrollTable];
            
            
            
            
            NSMutableArray *array = respondes;
            
            for (NSMutableDictionary *dic in array) {
                self.scroll = [[ScrollModel alloc] init];
                self.scroll.img = [dic objectForKey:@"img"];
                self.scroll.myId = [dic objectForKey:@"id"];
                [self.myArray addObject:self.scroll];
                
                
                
                [[HeroBaseHandle ShareHeroHeroDate]insertScroll:self.scroll];
                
            }

            
            self.myScroll = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
            self.myScroll.youDelegate =self;
            self.myScroll.backgroundColor = [UIColor whiteColor];
            [self.myView addSubview:self.myScroll];
            
           
                
                if (self.myArray.count != 0) {
                [self.myScroll setImages:self.myArray];
                }
                
        
            [self.myTableV.header endRefreshing];
            
        }else if ([respondes isEqualToString:@"error"]) {
            
            
            
            
            
        }
        
        
        
        
    }];


 
    
    
}
-(void)addfooter
{
    self.myTableV.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downAction)];
}


-(void)downAction
{
    
    if (self.number < 6) {
        self.number++;
        [self getInformation];
        return;
    }
    
    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"亲,没有更多内容了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertV show];
    
    [self.myTableV.footer endRefreshing];
  
    
    
    
}

-(void)careate
{
    self.number = 1;
    [self getInformation];
    [self create];
    
}


-(void)passId:(NSString *)name
{
    
    ViewController *myView = [[ViewController alloc] init];
    myView.newsId = name;
    
    [self.navigationController pushViewController:myView animated:YES];
   
    
}



-(void)getInformation
{
    
   MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    HUD.labelText = @"HHZ小组正卖力加载中";
    [self.navigationController.view addSubview:HUD];
    
       [HUD show:YES];
    
    [[HeroBaseHandle ShareHeroHeroDate]openDB];
    
    if ([[HeroBaseHandle ShareHeroHeroDate] selectNews] != nil) {
        self.array = [[HeroBaseHandle ShareHeroHeroDate] selectNews];

    }
    
    
    NSString *string = [NSString stringWithFormat:@"http://lolbox.oss.aliyuncs.com/json/v3/news/newslist_99_%ld.json?r=234800",(long)self.number];
    
    [AFN AFNGetData:string andBlock:^(id respondes) {
     
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            
            if (self.number == 1) {
                [[HeroBaseHandle ShareHeroHeroDate]openDB];
            
                [[HeroBaseHandle ShareHeroHeroDate] dropNewsTable];
         
                [[HeroBaseHandle ShareHeroHeroDate]createNewsTable];
            
                [self.array removeAllObjects];
                
            }
            NSMutableArray *array = respondes;
            
            for (NSMutableDictionary *dic in array) {
                self.news = [[NewsModel alloc] init];
                self.news.desc = [dic objectForKey:@"desc"];
                self.news.img = [dic objectForKey:@"img"];
                self.news.posttime = [dic objectForKey:@"posttime"];
                self.news.site = [dic objectForKey:@"site"];
                self.news.thumb_img = [dic objectForKey:@"thumb_img"];
                self.news.thumb_ok = [dic objectForKey:@"thumb_ok"];
                self.news.title = [dic objectForKey:@"title"];
                self.news.topic_id = [dic objectForKey:@"topic_id"];
                 self.news.newid = [dic objectForKey:@"id"];
            
                [HUD hide:YES];
                [HUD removeFromSuperview];
                [self.array addObject:self.news];
                 [[HeroBaseHandle ShareHeroHeroDate]insertNews:self.news];
               
           
                [_news release];
            }
            [self.myTableV.header endRefreshing];
            [self.myTableV.footer endRefreshing];
           
            [self.myTableV reloadData];
         

                      
            
            
            
        }else if ([respondes isEqualToString:@"error"]) {
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            
            [self.myTableV.header endRefreshing];
            [HUD hide:YES];
        
        }
            
            
        
       

    }];
 
   
    
    
    
    

    
    [self.myTableV reloadData];
    
    
    
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    ViewController *viewVC = [[ViewController alloc] init];

   [self.navigationController pushViewController:viewVC animated:YES];
   self.news = [self.array objectAtIndex:indexPath.row];
    
    viewVC.newsId = self.news.newid;

    

    NewsModel *news = [self.array objectAtIndex:indexPath.row];
    
       viewVC.newsId = news.newid;
    
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 116.337879;
   

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.array.count;
 
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"cell";
   NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer ];
    
    if (cell == nil) {
        
        cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        
        
    }
    NewsModel *news = [self.array objectAtIndex:indexPath.row];

    cell.News = news;
    return cell;
    
}
    
    
    
    
    
    
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
